<?php
class Panen{
	public $no_pancang; // id	
	public $no_individu;
	public $label_tandan; // set of label tandan produced by $no_individu
	
	public $total_bunga;
	public $total_polinasi;
	
	public $total_tandan_masuk;
	public $rataan_biji;
	public $rasio_seleksi;
	
	function Panen($db,$id){
		$this->no_pancang = $id;
		$this->db = $db;
		$this->process();
	}
	
	function getNoIndividu($no_pancang){
		$q = "SELECT `no_individu` FROM `caramel_managerial`.`pancang` WHERE `no_pancang`='$no_pancang'";
		
		return $this->db->get_var($q);
	}
	
	private function process(){
		$this->no_individu = $this->getNoIndividu($this->no_pancang);
		$year = '2008-01-01';
		if( $this->no_individu ){
			// total bunga			
			$this->total_bunga  = $this->getTotalBunga();
			
			// total polinasi
			$this->total_polinasi = $this->getTotalPolinasi();
			
			// fetch label tandan
			$this->label_tandan = $this->getLabelTandanSet();
			
			// total tandan masuk
			$this->total_tandan_masuk  = $this->getTotalTandanMasuk();
			
			// rataan biji
			$this->rataan_biji  = $this->getAverageBijiMasuk();
			
			// rasio seleksi biji
			$total_seleksi	    = $this->getTotalBijiSeleksi();
			if( $this->total_tandan_masuk != 0 )
				$this->rasio_seleksi = round($total_seleksi / $this->total_tandan_masuk,2);
		}
		else{
			$this->no_individu = -1;
			$this->total_bunga = -1;
			$this->total_polinasi= -1;

			$this->total_tandan_masuk= -1;
			$this->rataan_biji= -1;
			$this->rasio_seleksi= -1;
		}
	}
	
	private function getTotalBunga(){
		if( in_array($this->no_pancang, array('01229','01212')) ){
			$query = "SELECT SUM(jml_jantan) FROM `caramel_operational`.`bunga_pohon` WHERE `no_individu`='".$this->no_individu."'";
		}
		else{
			$query = "SELECT SUM(jml_betina) FROM `caramel_operational`.`bunga_pohon` WHERE `no_individu`='".$this->no_individu."'";
		}
		
		return $this->db->get_var($query);
	}
	
	private function getTotalPolinasi(){
		$query = "SELECT COUNT(*) FROM `caramel_operational`.`polinasi` WHERE `no_individu`='".$this->no_individu."'";
		return $this->db->get_var($query);	
	}
	
	private function getLabelTandanSet(){
		$query  = "SELECT `label_tandan` FROM `caramel_operational`.`polinasi` WHERE `no_individu`='".$this->no_individu."'";
		
		return  $this->db->get_results($query, ARRAY_A);
	}
	
	private function getAverageBijiMasuk(){
		if( count($this->label_tandan) > 0 ){
			$avg = 0;
			foreach($this->label_tandan as $label){
				$label_tandan = $label['label_tandan'];
				$query = "SELECT jml_biji_terima FROM `caramel_operational`.`inv_pencacahan` WHERE `label_tandan`='$label_tandan'";
				$avg += $this->db->get_var($query);
			}
			
			return round($avg/count($this->label_tandan));
		}
		else{
			return -1;
		}			
	}
	
	private function getTotalBijiSeleksi(){
		if( count($this->label_tandan) > 0 ){
			
			$sum = 0;
			foreach($this->label_tandan as $label){
				$label_tandan = $label['label_tandan'];
				$query = "SELECT jml_biji_saleable FROM `caramel_operational`.`inv_seleksi_akhir` WHERE `label_tandan`='$label_tandan'";
				$sum += $this->db->get_var($query);
			}
			
			return $sum;
		}
		else{
			return -1;
		}			
	}
	
	private function getTotalTandanMasuk(){
		if( count($this->label_tandan) > 0 ){
			
			$sum = 0;
			foreach($this->label_tandan as $label){
				$label_tandan = $label['label_tandan'];
				$query = "SELECT jml_biji_terima FROM `caramel_operational`.`inv_pencacahan` WHERE `label_tandan`='$label_tandan'";
				$sum += $this->db->get_var($query);
			}
			
			return $sum;
		}
		else{
			return -1;
		}			
	}
}
?>